import dayjs from 'dayjs'
Page({
  data: {
    currentDate: new Date().getTime(),
    nimDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    description: '',
    mobile: '',
    appointment: '',
    houseList: [],
    houseItem: {},
    repairItem: [],
    repairItem: {},
    attachment: [{
        url: '/repair_pkg/static/uploads/attachment.jpg'
      },
      {
        url: '/repair_pkg/static/uploads/attachment.jpg'
      },
    ],
  },
  async goList() {
    // 在这里发请求提交报修，和跳转页面之前 先校验，再去做
    if (!this.validateHouse() || !this.validateRepaire() || !this.validateMobile() || !this.validateAppointment() || !this.validateDescription() || !this.validateAttachment()) {
      return
    }
    let tmp = {
      houseId: this.data.houseItem.id,
      repairItemId: this.data.repairItem.id,
      mobile: this.data.mobile,
      appointment: this.data.appointment,
      description: this.data.description,
      attachment: this.data.attachment
    }
    if(this.data.id) {
      tmp = { ...tmp,id:this.data.id}
    }
    const res = await wx.http.post('/repair', tmp)
    console.log(res);
    if (res.code !== 10000) {
      wx.utils.toast(res.message || '提交报修失败')
      return
    }
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  onLoad({
    id
  }) {
    if (id) {
      this.getDetailInEdit(id)
    }
    this.getHouse()
    this.getRepaireList()

  },
  async getDetailInEdit(id) {
    wx.setNavigationBarTitle({
      title: '编辑报修信息'
    })
    const res = await wx.http.get(`/repair/${id}`)
    this.setData(Object.assign({}, {
      ...res.data
    }, {
      houseItem: {
        id: res.data.houseId,
        name: res.data.houseInfo
      },
      repairItem: {
        id: res.data.repairItemId,
        name: res.data.repairItemName
      }
    }))
  },
  // 上传图片
  afterRead(e) {
    wx.uploadFile({
      filePath: e.detail.file.url,
      name: 'file',
      url: 'https://live-api.itheima.net/upload',
      header: {
        Authorization: getApp().token
      },
      success: (res) => {
        const data = JSON.parse(res.data)
        this.data.attachment.push({
          url: data.data.url
        })
        this.setData({
          attachment: this.data.attachment
        })
      }
    })
  },
  // 删除图片
  deleteImage(e) {
    console.log(e);
    this.data.attachment.splice(e.detail.index, 1)
    this.setData({
      attachment: this.data.attachment
    })
  },
  // 获取房屋列表
  async getHouse() {
    const res = await wx.http.get('/room')
    const houseList = res.data.map(item => ({
      id: item.id,
      name: item.point
    }))
    this.setData({
      houseList
    })
  },
  // 获取维修项目列表
  async getRepaireList() {
    const res = await wx.http.get('/repairItem')
    this.setData({
      repairList: res.data
    })
  },
  // 选择房屋后绑定数据
  houseSelect(e) {
    console.log(e);
    this.setData({
      houseItem: e.detail
    })
  },
  // 选择维修项目后绑定数据
  repaireSelect(e) {
    console.log(e);
    this.setData({
      repairItem: e.detail
    })
  },
  // 实践选择函数
  datetimeConfirm(e) {
    this.setData({
      appointment: dayjs(e.detail).format('YYYY-MM-DD'),
      currentDate: e.detail,
      dateLayerVisible: false
    })
  },
  // 表单校验
  validateHouse() {
    if (!this.data.houseItem.id) {
      wx.utils.toast('必须选择报修房屋')
      return false
    }
    return true
  },
  validateRepaire() {
    if (!this.data.repairItem.id) {
      wx.utils.toast('必须选择维修项目')
      return false
    }
    return true
  },
  validateMobile() {
    const patterrn = /^1[3-9]\d{9}$/
    if (!patterrn.test(this.data.mobile)) {
      wx.utils.toast('手机号格式不对')
      return false
    }
    return true
  },
  validateAppointment() {
    if (!this.data.appointment) {
      wx.utils.toast('预约日期必填')
      return false
    }
    return true
  },
  validateDescription() {
    if (!this.data.description) {
      wx.utils.toast('问题描述必填')
      return false
    }
    if (this.data.description.length > 200) {
      wx.utils.toast('问题描述字数不能大于200')
      return false
    }
    return true
  },
  validateAttachment() {
    if (this.data.attachment.length > 9) {
      wx.utils.toast('问题附件最多传递9个图片')
      return false
    }
    return true
  },
  // 表单校验结束
  openHouseLayer(e) {
    this.setData({
      appointment: dayjs(e.detail).format('YYYY-MM-DD'),
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },

})